Skip to content

Add Zcash wallet core integration - #1563

Open
sneurlax wants to merge 3 commits into
stagingfrom
feat/zcash
Open

sneurlax wants to merge 3 commits into
stagingfrom
feat/zcash

Conversation

@sneurlax

@sneurlax sneurlax commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Add Zcash and tZcash coin definitions, wallet registration, node handling over lightwalletd, sync, reorg recovery, rescan, lifecycle, deletion, transaction and balance mapping, sending, and the native library check.
  • Implement the wallet through the zcash_flutter package only. Its build hook compiles the zcash-ffi Rust library.
  • Add unit coverage for the coin, address types, wallet identity, the node session, wallet state and its encrypted persistence, sync and reorgs, transparent receiver discovery and history, pending sends, send preparation and broadcast, and history and balance mapping.

Zcash is not added to the app's coin list here. The views it needs come with the UI PR at the top of this stack, along with the coin list entry, the README line and the wallet creation matrix entry. Until then the ZCASH flag builds and bundles zcash-ffi and nothing opens a Zcash wallet.

This is the first of a stack: this PR, then end-to-end tests against a local regtest chain, then the UI, then the Sapling parameter download.

What the wallet does

  • One account per wallet from a BIP39 phrase, with a Unified Address (Ironwood, Sapling and transparent receivers).
  • Sync through lightwalletd compact blocks, scanned in an isolate. State is saved encrypted (AES-GCM, key in secure storage) every ten ranges and at the tip, so an interrupted sync resumes.
  • Restore and rescan (recover) connect to the node, check the birthday against it and return. The scan is the wallet's next refresh, which recover starts without waiting for it, as the Monero wallets do: from an old birthday it takes a long time.
  • Balance: every pool counts towards the total. Spendable is the unspent Ironwood notes. Sapling, Orchard and transparent funds are shown as part of the total and cannot be spent by this PR.
  • Send: one or several recipients, to Unified, Sapling and transparent addresses, funded from Ironwood notes. A memo for a single shielded recipient. Notes of a send that was submitted and is not mined stay reserved until it is mined or expires.
  • A send keeps a durable record from preparation to confirmation, so an interrupted submission is never treated as either sent or not sent without evidence.

Commits

  1. style: dart format address.dart is formatter output only. That file was last changed before the SDK constraint moved to 3.13, so CI's format check fails on any change to it. Running dart format on the staging version reproduces this commit.
  2. feat: add Zcash wallet through zcash_flutter is the change.
  3. fix: anchor Zcash sends below the tip and settle failed and stale sends (5 files, +357/-31) fixes four issues found in review of commit 2:
    • A send took its anchor from the tip block. If that block was replaced, the transaction could never be mined and held its notes until it expired. A send now anchors three blocks below the tip (zcashAnchorDepth), spends only notes that deep, and a younger note shows as pending.
    • After the node refused a send, or a prepared send expired before it was submitted, the wallet still treated it as the prepared send. Confirming it again failed with a misleading error. It is now cleared. A send whose submission had no answer stays prepared, so confirming again submits the same transaction.
    • Pending-send records were never removed. Records of sends that expired without being mined are now dropped at the next preparation. The record of a mined send stays, because its history row takes the fee and recipients from it.
    • While a send to the wallet's own address was not mined, the total balance dropped by the amount as well as the fee. Only the fee leaves now.

Size

The feature commit is 40 files, +11457/-6. New Zcash library files are about 4.7k lines and new tests and fakes about 6.7k. Existing files get about +60/-6: the coin's address types in Address (and its generated file), the price id, the reset list, the native library check, the wallet's files and keys deleted in services/wallets.dart, a test-only coin list override in AppConfig, the ZCASH flag in configure_stack_wallet.sh, the dependency in the pubspec template, and PLACEHOLDER_IF_<marker> substitution in tool/process_pubspec_deps.dart, which lets a pubspec setting outside a marker's block follow the marker.

Known limits

  • Paying a Sapling address needs the Sapling proving parameters. This PR has no source for them, so such a send is refused with a message that says so. A later PR downloads them.
  • Sapling, Orchard and transparent funds cannot be spent yet. The wallet's Unified Address has Sapling and transparent receivers (the backend requires them), so a payer can choose them and the funds then wait for the later PR that moves them to Ironwood.
  • A send fetches the witnesses of the notes it spends by downloading the compact blocks from the oldest of those notes to the tip. The server therefore learns the height of that note, and spending an old note downloads many blocks in one request. Keeping witnesses current during sync needs support in zcash_flutter.
  • A received note, and the change of a send, can be spent once it is three blocks below the tip, about four minutes. Until then it shows as pending.
  • A long scan shows as syncing without a percentage: the refresh reports the height when it reaches the tip.
  • Proving runs on the UI isolate, because the backend's build call is synchronous. A one-note send measured about 0.8 s on this machine.
  • The fee the send form shows before a send is prepared is an estimate for one Unified-address recipient, from the notes that are free to spend. A transparent or Sapling recipient, several recipients, or a different choice of notes by the builder can make the real fee higher. The fee of the prepared send, shown at confirmation, is exact.
  • There is no send-all: the user leaves the fee.
  • The connection to lightwalletd is not routed through Tor (torSupport is false).
  • recoverPreparedSend rebuilds a send that was left prepared or with an unknown outcome so it can be confirmed again. No view calls it yet. Without it, the next prepareSend frees the notes of a send that was only prepared, and one whose submission was interrupted is listed as unconfirmed until it is mined or expires.

Not changed here

  • At startup, if one wallet's init() throws, services/wallets.dart skips the startup refresh for every wallet. This predates this PR and affects all coins.
  • TransactionV2.getFee reads the fee override that the Zcash mapper sets for shielded sends. A send found by a restore has no record of its fee, so it shows fee 0 and the amount includes the fee.

Dependency boundary

zcash_flutter resolves from https://github.com/ManyMath/zcash at c8bc869ba9ba0227f944329df9e875d2a55c5983, path packages/zcash_flutter, and the zcash package it depends on is overridden to the same commit, path packages/zcash. The hook builds zcash-ffi with the Rust toolchain that the package's rust-toolchain.toml pins (1.99.1), from crates.io sources.

zcash_flutter is outside ENABLE_ZCASH in the pubspec template because lib/ uses the API it re-exports directly. The ZCASH flag decides whether the hook builds the library at all (hooks.user_defines.zcash_flutter.enabled).

Verification

At this PR's head (8e085fe73):

  • flutter analyze on the changed Dart files: no errors, and no new warnings (the ones reported are in staging's services/wallets.dart and wallets/wallet/wallet.dart).
  • CI's format check on the PR's changed Dart files: clean.
  • The Zcash unit tests (test/wallets/zcash_*_test.dart and the native library check): 138 pass. CI builds zcash-ffi through the hook, as it configures the app with build_app.sh.
  • New with commit 3: the anchor height a send asks for and its refusal of a note above it, the pending balance of a young note at the heights on both sides of the boundary, a refused send no longer being prepared (through a real wallet over a stored send), a send without an answer being resubmitted with the same bytes, the pruning of expired records against mined, unexpired and prepared ones, and the balance of a send to the wallet's own address.
  • The full flutter test suite, run at the head of Add a Zcash migration view to the More menu #1569 (the top of the stack, which contains this head): 1595 passed, 8 skipped.
  • The end-to-end suite from Add Zcash regtest end-to-end test coverage #1564 against zebrad 7.0.0 and lightwalletd 0.6.1, run at the head of Add a Zcash migration view to the More menu #1569: 1/1. It now checks that a note is pending and cannot be spent in the block that holds it, and is spendable three blocks later.
  • The eight scripts/tests that CI runs passed at the previous head (c92fd8f96) and were not run again: commit 3 changes three wallet files and two tests.
  • The tests for a send left behind by an interrupted app and for a wallet opened during its exit were checked against the code without their fixes, and fail there.

Not covered by a test: the clearing of a prepared send that expired before it was submitted (the wallet's fake node has a fixed tip).

The file predates the formatter that CI's changed-file check runs, so any
change to it fails that check. Formatting only.
@codecov

codecov Bot commented Dec 3, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 83.79630% with 315 lines in your changes missing coverage. Please review.
✅ Project coverage is 21.80%. Comparing base (dcd2d9a) to head (c92fd8f).
⚠️ Report is 11 commits behind head on staging.

Files with missing lines Patch % Lines
...ets/wallet/intermediate/zcash_account_session.dart 65.88% 87 Missing ⚠️
lib/wallets/wallet/impl/zcash_wallet.dart 86.09% 79 Missing ⚠️
...et/intermediate/zcash_wallet_state_controller.dart 86.70% 69 Missing ⚠️
lib/wallets/wallet/intermediate/zcash_send.dart 82.41% 48 Missing ⚠️
lib/wallets/crypto_currency/coins/zcash.dart 71.23% 21 Missing ⚠️
...ib/models/isar/models/blockchain_data/address.dart 0.00% 3 Missing ⚠️
...rmediate/zcash_transparent_receiver_discovery.dart 88.00% 3 Missing ⚠️
lib/services/wallets.dart 33.33% 2 Missing ⚠️
...allets/wallet/intermediate/zcash_wallet_state.dart 98.21% 2 Missing ⚠️
.../wallet/intermediate/zcash_wallet_persistence.dart 97.56% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           staging    #1563      +/-   ##
===========================================
+ Coverage    20.67%   21.80%   +1.13%     
===========================================
  Files         1148     1157       +9     
  Lines       114416   116360    +1944     
===========================================
+ Hits         23653    25374    +1721     
- Misses       90763    90986     +223     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

- Take the anchor of a send three blocks below the tip, spend only notes
  that deep, and show younger notes as pending.
- A send the node refuses, or that expired before it was submitted, is no
  longer the prepared send.
- Drop the pending records of sends that expired without being mined.
- Keep what an unmined send pays to the wallet's own address in the total
  balance.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant